﻿using UnityEngine;
using System.Collections;

public class Food : MonoBehaviour {

    public GameObject foodGo;
    public FoodView foodView;
    public FoodInfo foodInfo;
    public int foodPosX;
    public int foodPosY;
    public bool isAttack = false;
    public byte Energy = 0;
    public SkillInfo skillInfo;
    public int dodge = 100;


    public ArrayList AttackRangeMapGridID = new ArrayList();

    /// <summary>
    /// 初始化赋值，初始化位置
    /// </summary>
    /// <param name="id"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void Init(int id, int x, int y)
    {
        foodPosX = x;
        foodPosY = y;
        foodInfo = ReadXml._instance.GetFoodById(id);
        foodGo = Instantiate(Resources.Load("Prefabs/" + foodInfo.SpriteName)) as GameObject;
        int MapGridID = MapManager._instance.CalcuMapGridID(x, y);
        GameObject MapGrid = MapManager._instance.GetMapGridGo(MapGridID);
        foodView = foodGo.GetComponent<FoodView>();
        foodView.SetViewPos(MapGrid);
        AttackRange();
        skillInfo = ReadXml._instance.GetSkillByID(foodInfo.SkillID);
        //Debug.Log(skillInfo.skillname);
    }

    /// <summary>
    /// 刷新位置
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public void SetGoPos(int x, int y)
    {
        foodPosX = x;
        foodPosY = y;
        int MapGridID = MapManager._instance.CalcuMapGridID(x, y);
        GameObject MapGrid = MapManager._instance.GetMapGridGo(MapGridID);
        foodView.SetViewPos(MapGrid);
        AttackRange();
    }

    /// <summary>
    /// 给食物的攻击范围赋值
    /// </summary>
    private void AttackRange()
    {
        AttackRangeMapGridID.Clear();
        int attackRangePosX = 0;
        int attackRangePosY = 0;
        //Debug.Log(foodInfo.AttackRange.Length);
        for (int i = 0; i < foodInfo.AttackRange.Length; i += 2)
        {
            attackRangePosX = foodInfo.AttackRange[i] + foodPosX;
            attackRangePosY = foodInfo.AttackRange[i + 1] + foodPosY;
            //Debug.Log(MapManager._instance.MapX + "   " + attackRangePosY);
            if (attackRangePosX >= 0 && attackRangePosX < MapManager._instance.MapX && attackRangePosY >= 0 && attackRangePosY < MapManager._instance.MapY)
            {
                int MapGridID = MapManager._instance.CalcuMapGridID(attackRangePosX, attackRangePosY);
                AttackRangeMapGridID.Add(MapGridID);
                //Debug.Log(i + "  " + MapGridID + "  " + AttackRangeMapGridID[a] + "  " + a);
            }
        }
        //Debug.Log(AttackRangeMapGridID[0]);
    }


    /// <summary>
    /// 受伤计算
    /// </summary>
    /// <param name="damage"></param>
    public void OnDamage(int damage)
    {
        float tempDodge = Random.Range(0f, 100f);

        if (tempDodge <= dodge)
        {
            if (foodGo.tag == "PlayerFood")
            {
                TeamManager._instance.CurrenPlayerHealth -= damage;
            }
            else
            {
                TeamManager._instance.CurrenEnemyHealth -= damage;
            }
            foodView.ShowDamage(damage.ToString(), Color.red);
            HealthInfoView._instance.UpdateHealth();
            Energy++;
            foodView.SetEnergyView(Energy);
        }
        else
        {
            foodView.ShowDamage("闪避", Color.green);
        }
    }
}
